#!/usr/bin/env bash

CURRDIR=$(realpath $(dirname $0))
DATADIR="$CURRDIR/data"
CSVOUTPUT="$DATADIR/output.csv"
DATADIR_OVPNS="$DATADIR/ovpns"
csv_offset=2

## Default config values

# How many connections to make available.
ovpn_count=5
# After how many hours the downloaded VPN connection list should be considered
# old/stale and latest file should be (offered to be) downloaded.
stale_threshold_hours=6
# If set to true, update stale files without asking.
auto_update_when_stale=true

# Try to source config.sh if it exists.
# If you want to override default config values try
#   cp -i config.sample.sh config.sh
# and edit config.sh.
[ -f "$CURRDIR/config.sh" ] && source "$CURRDIR/config.sh"

# Detects if a file is old enough depending on a threshold of hours.
function is_file_stale() {
	local filename="$1"
	# Redirection is to suppress output, but later get return code
	stat -q > /dev/null 2>&1
	if [ "$?" = '1' ]; then # GNU+Linux doesn't have -q option
		local changed=$(stat -c "%Y" "$filename")
	else # BSD etc.
		local changed=$(stat -f "%m" "$filename")
	fi
	local now=$(date +%s)

	# Difference between now and file modified time is more than the threshold
	if [ $[ $now - $changed ] -gt $[ $stale_threshold_hours * 60 * 60 ] ]; then
		return 1
	else
		return 0
	fi
}
